package com.hnue.k60b.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.hnue.k60b.models.RssItem;

//xu ly xml
public class RssHandle extends DefaultHandler {

	public List<RssItem> getItemlist() {
		return itemlist;
	}

	public static final String ITEM = "item";
	public static final String TITLE = "title";
	public static final String DESCRIPTION = "description";
	public static final String LINK = "link";
	public static final String DATE = "pubDate";
	private boolean starst = false;

	private RssItem item;
	private List<RssItem> itemlist = new ArrayList<RssItem>();
	private StringBuilder sbuider = new StringBuilder();

	// lay chuoi ki tu
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if (localName.equalsIgnoreCase(ITEM)) {
			item = new RssItem();
			starst = true;

		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if (starst && (sbuider != null)) {
			sbuider.append(ch, start, length);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if (localName.equalsIgnoreCase(ITEM)) {
			itemlist.add(item);

		} else if (starst) {
			if (localName.equalsIgnoreCase(TITLE)) {
				item.setTitle(sbuider.toString().trim());

			} else if (localName.equalsIgnoreCase(DESCRIPTION)) {
				item.setDescription(sbuider.toString().trim());

			} else if (localName.equalsIgnoreCase(LINK)) {
				item.setLink(sbuider.toString().trim());

			} else if (localName.equalsIgnoreCase(DATE)) {
				item.setDate(sbuider.toString().trim());

			}
			// sau moi tin, f5 lai sbuider
			sbuider = new StringBuilder();
		}
	}

}
